package com.futureCloudMOM.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.futureCloudMOM.common.exception.base.BaseException;
import com.futureCloudMOM.core.base.service.impl.BaseServiceImpl;
import com.futureCloudMOM.core.domain.BasicRoutingParam;
import com.futureCloudMOM.core.domain.BasicRoutingProcess;
import com.futureCloudMOM.core.domain.ProduceTaskBillRoutingParam;
import com.futureCloudMOM.core.mapper.ProduceTaskBillRoutingParamMapper;
import com.futureCloudMOM.core.service.BasicRoutingParamService;
import com.futureCloudMOM.core.service.ProduceTaskBillRoutingParamService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 【Produce_TaskBillRoutingParam】的数据库操作Service实现
 *
 * @author Administrator
 * @since 2023-06-06 16:03:15
 */
@Service
@AllArgsConstructor
public class ProduceTaskBillRoutingParamServiceImpl extends BaseServiceImpl<ProduceTaskBillRoutingParamMapper, ProduceTaskBillRoutingParam> implements ProduceTaskBillRoutingParamService {

    private final BasicRoutingParamService basicRoutingParamService;

    @Override
    public List<ProduceTaskBillRoutingParam> selectListByProcessId(Long processId) {
        return list(new LambdaQueryWrapper<ProduceTaskBillRoutingParam>()
                .eq(ProduceTaskBillRoutingParam::getTaskBillRoutingProcessId, processId));
    }

    @Override
    public void create(Long billProcessId, BasicRoutingProcess basicRoutingProcess) {
        List<BasicRoutingParam> routingParams = basicRoutingParamService.selectListByProcessId(basicRoutingProcess.getId());
        if (CollectionUtils.isEmpty(routingParams)) {
            throw new BaseException("序号为：" + basicRoutingProcess.getSubNo() + "的工序，加工参数信息未维护");
        }
         /*
         保存订单工序加工参数
         */
        routingParams.forEach(basicRoutingParam -> {
            ProduceTaskBillRoutingParam taskBillRoutingMaterial = ProduceTaskBillRoutingParam.getInstance(basicRoutingParam);
            taskBillRoutingMaterial.setTaskBillRoutingProcessId(billProcessId);
            save(taskBillRoutingMaterial);
        });
    }

    @Override
    public void deleteByProcessId(Long processId) {
        getBaseMapper().delete(new LambdaQueryWrapper<ProduceTaskBillRoutingParam>()
                .eq(ProduceTaskBillRoutingParam::getTaskBillRoutingProcessId, processId));
    }

}
